package cn.schoolwow.ams.test.module.api.aware;

import cn.schoolwow.ams.domain.block.list.clazz.ClazzListBlockConfigAware;
import cn.schoolwow.ams.domain.block.list.clazz.ClazzListBlockOption;
import cn.schoolwow.ams.domain.block.list.clazz.crud.ClazzListBlockListListener;
import cn.schoolwow.ams.domain.block.list.common.ListBlockPagingResponse;
import cn.schoolwow.ams.module.block.listBlock.common.domain.remoteSelect.BlockRemoteSelect;
import cn.schoolwow.ams.module.block.listBlock.common.domain.remoteSelect.RemoteSelectOption;
import cn.schoolwow.ams.module.router.domain.BlockRouterPush;
import cn.schoolwow.ams.test.module.api.domain.API;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class APIAware extends ClazzListBlockConfigAware {
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public ClazzListBlockOption getAMSBlockConfig() {
        ClazzListBlockOption clazzListBlockOption = new ClazzListBlockOption("test", "API", API.class);
        clazzListBlockOption.clazzListBlockAPIListener.listListener = new ClazzListBlockListListener() {
            @Override
            public ListBlockPagingResponse list(JSONObject conditionMap) throws Exception {
                API api = new API();
                api.type = "公共";
                api.url = "http://127.0.0.1";
                api.requestMethod = "GET";
                api.contentType = "form";
                api.requestBody = "a=1&b=2";
                return new ListBlockPagingResponse(Arrays.asList(api));
            }
        };
        {
            List<RemoteSelectOption> list = new ArrayList<>();
            for(int i=0;i<5;i++){
                RemoteSelectOption remoteSelectOption = new RemoteSelectOption("选项"+i, i+"");
                list.add(remoteSelectOption);

                remoteSelectOption.children = new ArrayList<>();
                for(int j=0;j<3;j++){
                    RemoteSelectOption childRemoteSelectOption = new RemoteSelectOption("选项"+i+"-子选项"+j, j+"");
                    remoteSelectOption.children.add(childRemoteSelectOption);
                }
            }
            BlockRemoteSelect blockRemoteSelect = new BlockRemoteSelect("type", "字段", list);
            blockRemoteSelect.cascade = true;
            clazzListBlockOption.remoteSelectList.add(blockRemoteSelect);
        }
        {
            BlockRouterPush blockRouterPush = new BlockRouterPush("执行请求", applicationContext.getBean(ExecuteAPIAware.class));
            blockRouterPush.sourceTargetFieldNameMap.put("requestBody", "body");
            clazzListBlockOption.blockRouterPushList.add(blockRouterPush);
        }
        return clazzListBlockOption;
    }
}
